<%@page import="com.pay.ips7.server.MD5"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.pay.yeepay.client.service.YeepayService"%>
<%@page import="com.vo.Gateway"%>
<%@page import="org.dom4j.Document"%>
<%@page import="org.dom4j.DocumentHelper"%>
<%@page import="org.dom4j.Element"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
	String resultXml = request.getParameter("paymentResult");
	Gateway gateway = Gateway.dao.findById(19);
	String MerId = gateway.getStr("gateway_merid");	//商户ID
	String MD5key = gateway.getStr("gateway_key");	//商户密匙

// xml截取
	String strxml = resultXml.substring(resultXml.lastIndexOf("<body>"),
			resultXml.lastIndexOf("</body>") + 7);

	String md5Str = strxml + MerId + MD5key;
	String MD5info = com.ips.commons.security.MD5.sign(md5Str);
	//MD5 md5 = new MD5();
	//String MD5info = md5.getMD5ofStr(md5Str).toLowerCase());

//返回xml处理
	Document doc = null;
	try {
		doc = DocumentHelper.parseText(resultXml);
		Element root = doc.getRootElement();
		Element headElement = root.element("GateWayRsp").element("head");
		Element bodyElement = root.element("GateWayRsp").element("body");

// 以下是head部分
		String ReferenceId = headElement.elementText("ReferenceId");
		String RspCode = headElement.elementText("RspCode");
		String RspMsg = headElement.elementText("RspMsg");
		String ReqDate = headElement.elementText("ReqDate");
		String RspDate = headElement.elementText("RspDate");
		String Signature = headElement.elementText("Signature");

// 以下是body部分
		String MerBillNo = bodyElement.elementText("MerBillNo");
		String CurrencyType = bodyElement.elementText("CurrencyType");
		String Amount = bodyElement.elementText("Amount");
		String Date = bodyElement.elementText("Date");
		String Status = bodyElement.elementText("Status");
		String Msg = bodyElement.elementText("Msg");
		String Attach = bodyElement.elementText("Attach");
		String IpsBillNo = bodyElement.elementText("IpsBillNo");
		String IpsTradeNo = bodyElement.elementText("IpsTradeNo");
		String BankBillNo = bodyElement.elementText("BankBillNo");
		String RetEncodeType = bodyElement.elementText("RetEncodeType");
		String ResultType = bodyElement.elementText("ResultType");
		String IpsBillTime = bodyElement.elementText("IpsBillTime");

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	if (MD5info.equals(Signature)){
		if(Status.equals("Y")){
			YeepayService.service.netcallback("", MerBillNo, "", sdf.format(new Date()), sdf.format(new Date()));
			response.sendRedirect(basePath+"/yeepay/yeeReturn?r6_Order="+MerBillNo);
		}else{
			YeepayService.service.failure(MerBillNo, sdf.format(new Date()));
			response.sendRedirect(basePath+"/yeepay/yeeReturn?r6_Order="+MerBillNo);
		}
	}else{
		out.println("交易签名被篡改!");
	}
} catch (Exception e) {
}
%>